const { ccclass, property } = cc._decorator;
import MyEnums from '../MyEnums';
import TargetBase from './TargetBase';

class AttackItem {
    type: number;
    value: number;
    duration: number;
    dynamic: boolean;
    recovery: boolean;
}

@ccclass
export default class AttackManager extends cc.Component {
    @property
    StatusItem: AttackItem[] = [];
    @property
    RefresItem: AttackItem[] = [];
    @property
    damageAttack: number = 0;//基础伤害
    @property
    source: number = MyEnums.SkillSource.hero;//攻击发出者
    @property
    attribute: number = 0;//攻击类型(火，水，风，毒。。。。)
    onLoad() {

    }
    addStatusItem(_type: number, _value: number, _duration: number) {
        if (_value == 0)
            return;
        let hav = this.StatusItem.filter(t => (t.type == _type && t.duration == _duration));
        if (hav?.length > 0) {
            hav[0].value += _value;
        }
        else {
            let ai: AttackItem = new AttackItem();
            ai.type = _type;
            ai.value = _value;
            ai.duration = _duration;
            ai.recovery = true;
            ai.dynamic = false;
            this.StatusItem.push(ai);
        }
    }
    addRefresItem(_type: number, _value: number, _duration: number = 0, _dynamic: boolean = false, _recovery: boolean = false) {
        if (_value == 0)
            return;
        let hav = this.RefresItem.filter(t => (t.type == _type && t.duration == _duration && t.dynamic == _dynamic && t.recovery == _recovery));
        if (hav?.length > 0) {
            hav[0].value += _value;
        }
        else {
            let ai: AttackItem = new AttackItem();
            ai.type = _type;
            ai.value = _value;
            ai.duration = _duration;
            ai.recovery = _recovery;
            ai.dynamic = _dynamic;
            this.RefresItem.push(ai);
        }
    }
    //输出伤害
    doAttack(TargetNode: cc.Node, HitSource?: number, TargetControl?: TargetBase) {
        if (!TargetControl)
            TargetControl = TargetNode.getComponent(TargetBase);
        if (TargetControl) {
            let _default: AttackItem = null;
            if (this.source != HitSource && this.damageAttack != 0) {
                _default = {
                    type: MyEnums.SkillRefreshType.life,
                    value: this.damageAttack,
                    duration: 0,
                    dynamic: false,
                    recovery: false
                };
            }
            if (this.StatusItem.length > 0) {
                TargetControl.setStatusAttack(HitSource, this.attribute, this.StatusItem);
            }
            if (this.RefresItem?.length > 0 || _default) {
                TargetControl.setRefreshAttack(HitSource, this.attribute, this.RefresItem, _default);
            }
            if (HitSource == MyEnums.SkillSource.hero && this.source != HitSource)
                HeroManager.getInstance().HitCounts++;
            if (TargetControl._showLife < 0)
                TargetControl.playSound();
            TargetControl.showLife(TargetControl._showLife);
        }
        else {
            cc.log("输出伤害失败");
        }
    }
}
